package com.xq.shop.controller.admin;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.google.gson.ExclusionStrategy;
import com.google.gson.FieldAttributes;
import com.xq.shop.po.Admin;
import com.xq.shop.po.Role;
import com.xq.shop.service.admin.IAdminService;
import com.xq.shop.service.admin.IBaseService;
import com.xq.shop.service.admin.IRoleService;
import com.xq.shop.util.AdminServiceFactory;
import com.xq.shop.util.GsonUtil;
import com.xq.shop.vo.RespMsg;
import com.xq.shop.vo.SearchObject;
import com.xq.shop.vo.RespMsg.Result;

@Controller
@RequestMapping("/admin/")
public class AdminController extends BaseController<Admin>{

	private IAdminService adminService = AdminServiceFactory.getAdminService();
	private IRoleService roleService = AdminServiceFactory.getRoleService();
	
	@Override
	public IBaseService<Admin> getService() {
		return adminService;
	}
	
	@RequestMapping("admin_save.do")
	public ModelAndView save(Admin admin, Long[] roleIds, HttpServletResponse response) throws Exception{
		
		RespMsg msg;
		
		if(admin.getId() == null && adminService.usernameExists(admin.getUsername())){
			
			msg = new RespMsg(Result.error, "保存失败,用户名已存在");
		} else {
			
			admin.setRoles(new HashSet<Role>(roleService.get(roleIds)));
			
			if(admin.getId() != null){
				
				Admin tmp = adminService.get(admin.getId());
				admin.setUsername(tmp.getUsername());
			}
			
			if(StringUtils.isBlank(admin.getUsername())){
				
				msg = new RespMsg(Result.error, "保存失败,请输入用户名");
			} else {
				
				adminService.saveOrUpdate(admin);
				msg = new RespMsg(Result.success, "保存成功");
			}
		}
			
			
		ModelAndView modelAndView = new ModelAndView("admin/setting/admin_list");
		modelAndView.addObject(msg);
		return modelAndView;
	}
	
	@RequestMapping("check_username.do")
	@ResponseBody
	public String checkUsername(String username){
		 
		if (StringUtils.isEmpty(username)) return "false";
	    return adminService.usernameExists(username) ? "false" : "true";
	}
	
	@RequestMapping("admin_list.do")
	@ResponseBody
	public String list(SearchObject search){
		return GsonUtil.toJson(adminService.get(search), new ExclusionStrategy() {
			
			@Override
			public boolean shouldSkipField(FieldAttributes f) {
				return "roles".equals(f.getName());
			}
			
			@Override
			public boolean shouldSkipClass(Class<?> clazz) {
				return false;
			}
		});
	}
	
	@RequestMapping("admin_edit.do")
	public ModelAndView edit(String id, HttpServletResponse response){
		
		ModelAndView view = new ModelAndView("admin/setting/admin_edit");
		Admin admin;
		if(id != null){
			admin = adminService.get(Long.parseLong(id));
		} else {
			admin = new Admin();
		}
		
		if(admin == null){
			//TODO go to 404
			return view;
		} else {
			
			view.addObject("entity", admin);
			
			//获取所有角色信息
			List<Role> role_List = roleService.getAll();
			view.addObject("roleList", role_List);
			
			//添加操作员关联的角色
			String selected = "";
			Set<Role> role_Set = admin.getRoles();
			if(role_Set != null){
				
				for(Role role : role_Set){
					
					selected += role.getId() + ",";
				}
				
				if(selected.length() > 1) selected = selected.substring(0, selected.length() - 1);
			}
			view.addObject("selected", selected);
			
			return view;
		}
	}
	
	@RequestMapping("admin_delete.do")
	public void delete(String id, HttpServletResponse response){
		
		super.delete(id, response);
	}
}
